﻿#region Using Directives

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.IO.MemoryMappedFiles;
using Edinger.Controls;
using System.Text;
using Edinger.IO;
using System.Collections.Generic;

#endregion Using Directives


namespace Edinger.Forms
{
    internal sealed partial class MainForm : Form
    {
        #region Methods

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutForm aboutForm = new AboutForm())
                aboutForm.ShowDialog(this);
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }


        private void openForegroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string fileName = (string)e.Argument;
            long fileLength = new FileInfo(fileName).Length;

            openForegroundWorker.ReportProgress(0, string.Format("Reading {0:0.00} of {1:0.00} KB", 0, fileLength / 1024));

            FileStream stream = File.OpenRead(fileName);
            LineScanner lineScanner = new LineScanner(stream);

            List<long> lineOffsets = new List<long>();
            while (lineScanner.Read())
            {
                if (openForegroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                lineOffsets.Add(lineScanner.LineOffset);
                int percent = (int)(lineScanner.LineOffset * 100 / fileLength);
                openForegroundWorker.ReportProgress(percent, string.Format("Reading {0:0.00} of {1:0.00} KB", lineScanner.LineOffset / 1024, fileLength / 1024));
            }

            lineOffsets.Add(fileLength);
            openForegroundWorker.ReportProgress(100, string.Format("Reading {0:0.00} of {1:0.00} KB", fileLength / 1024, fileLength / 1024));

            TextEditorDocument ted = new TextEditorDocument(stream, lineOffsets);
            e.Result = ted;
        }


        private void openForegroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled || e.Error != null)
                return;

            mainTextEditor.Document = (TextEditorDocument)e.Result;
        }


        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (openFileDialog.ShowDialog(this) != DialogResult.OK)
                    return;

                openForegroundWorker.Description = "Opening " + Path.GetFileName(openFileDialog.FileName) + "...";
                openForegroundWorker.RunWorker(this, openFileDialog.FileName);
            }
        }

        #endregion Methods


        #region Constructors

        public MainForm()
        {
            InitializeComponent();

            Text = Application.ProductName;
            aboutToolStripMenuItem.Text = "&About " + Application.ProductName;
            openForegroundWorker.Title = Application.ProductName;
        }

        #endregion Constructors
    }
}
